<?php
namespace Home\Controller;
use Think\Controller;
class JaguarController extends Controller {
	
    public function reg(){
		$activelist = M('Active')->select();
		$this->assign('activelist',$activelist);
        $this->display();
    }
	
	public function getTime(){
		$city = I('get.city');
		$act = M('Active')->where(array('city'=>$city))->find();
		$data = array();
		$sTime = strtotime($act['starttime']);
		$eTime = strtotime($act['endtime']);
		$st = $sTime;
		while($st<=$eTime){
			$data[] = date('Y/m/d',$st);
			$st += 3600*24;
		}
		_Ajax(true,'OK',$data);
	}
    
	public function saveReg(){
		$data = I('post.');
  		$d = array(
			'username'	=>	$data['username'],
			'sex'	=>	$data['sex'],
			'phone'	=>	$data['phone'],
			'email'	=>	$data['email'],
			'buy_time'	=>	$data['buy_time'],
			'city'	=>	$data['city'],
			'comefrom'	=>	$data['comefrom'],
			'cartype'	=>	$data['cartype'],
			'act_index'	=>	$data['act_index'],
			'act_time'	=>	$data['act_time'],
			'group'	=>	1, 
		);
		$res=M('User')->where('phone='.$data['phone'])->find();
		if($res){
			_Ajax(1,'您已通过其他渠道注册,请直接登入');
		}else{
		$rs = M('User')->data($d)->add();
		if($rs){
			_Ajax(true,'注册成功');
		}else{
			_Ajax(false,'注册失败,请稍后重试');
		}
		}
	}
	
	public function info(){
        $this->display();
    }
	
	public function game(){
		$this->display('gamehasnotstart');
	}
	
	public function logout(){
		session('uid',null);
	}
	
    public function check(){
		$uid = session('uid');
		if(!empty($uid)){
			$this->assign('hasCheck',true);
			$user = M('User')->where('id='.$uid)->find();
			if(!empty($user)){
				$user['namestr'] = mb_substr($user['username'], 0, 1, 'utf8').$user['sex'];
				$user['img'] = '/index.php/Home/Jaguar/getEWM/uid/'.$user['id'];
			}
			$this->assign('user',$user);
		}
        $this->display();
    }
	
	public function docheck(){
		$phone = I('get.phone');
		$user = M('User')->where('phone="'.$phone.'"')->find();
		if(!empty($user)){
			session('uid',$user['id']);
			_Ajax(true,'验证完成',$user);
		}else{
			_Ajax(false,'手机号码不存在');
		}
	}
	
	public function getEWM(){
	    Vendor('Phpqrcode.phpqrcode');
	    // 如果没有http 则添加
		$uid = I('get.uid');
	    \QRcode::png($uid,false,QR_ECLEVEL_H,10,0,false,0xFFFFFF,0x000000); 
	}
	
	public function upimg(){
		$uid = session('uid');
		$row = M('Score')->where('uid='.$uid)->find();
		if(!empty($row)){
			$this->assign('img',$row['g4_value']);
		}
		$this->display();
	}
	
	public function doupimg(){ 
		$str = $_POST['imgdata'];  
		if(empty($str)){
			_Ajax(false,'图片上传失败');
		} 
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $str, $result)){ 
		  $type = $result[2];
		  
		  $new_file = "/Public/upload/".date('YmdHis').'_'.mt_rand(1000,9999).".{$type}"; 
		  if (file_put_contents('.'.$new_file, base64_decode(str_replace($result[1], '', $str)))){
 
			saveScore(array(
				'index'	=>	5,
				'value'	=>	$new_file,
				'score'	=>	100,
			));
			
			_Ajax(true,'图片上传成功',$new_file);
		  }else{
		  	_Ajax(false,'图片上传失败');
		  }
		}
		_Ajax(false,'图片上传失败');
		exit();
	}
	
	public function tiaozhan(){
		$id = I('get.t');
		if($id=='2'){
			$style="green_";
		}else{
			$style="red_";
		}
		$this->assign('style',$style);
		$this->display();
	}
	public function result(){
		$options=C('options');   		 
    	$weObj = new \Org\Util\Wechat($options);
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $sign = $weObj->getJsSign($url);	
        $this->assign('arr',$sign);
		 $uid=I('uid');
		if(empty($uid)){
		
			$uid = session('uid');
			if(empty($uid)){$this->redirect('Jaguar/check');}
		}
		$re = M('user')->where('id='.$uid)->find();
		$res = M('score')->where('uid='.$uid)->find();
		$fen=$res['g1_score']+$res['g2_score']+$res['g3_score']+$res['g4_score']+$res['g5_score'];
		
		
		$name = mb_substr($re['username'], 0, 1, 'utf8').$re['sex'];
		$this->assign('name',$name );
		$this->assign('fen',$fen);
		$this->assign('re',$re);
		$this->assign('res',$res);
        $this->display();
    }

}
